package com.lan.iot.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Rect;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.lan.iot.R;
import com.lan.iot.RecyAdapter;


public class VPFragment extends Fragment {
    private static final String ARG_PARAM1 = "title";
    private static final String ARG_PARAM2 = "img";
public Context context;
    private String title;
    private int img;
    private int roomid;
    public  VPFragment( ){}

    public  VPFragment(Context cont,int roomid){this.context=cont;this.roomid=roomid;}
    public static VPFragment newInstance(String title, int img,Context con,int roomid) {
        VPFragment fragment = new VPFragment(con,roomid);
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, title);
        args.putInt(ARG_PARAM2,img);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            title = getArguments().getString(ARG_PARAM1);
            img = getArguments().getInt(ARG_PARAM2);
        }
    }
  public    RecyAdapter mAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_v_p, container, false);
        Bundle argument = getArguments();
        RecyclerView mRecyclerView;
        mRecyclerView=view.findViewById(R.id.recycler_view);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2, LinearLayoutManager.VERTICAL,false);

        mRecyclerView.setLayoutManager(gridLayoutManager);
        mAdapter = new RecyAdapter(this.context,roomid);
        mRecyclerView.setAdapter(mAdapter);

        /**
         * 设置网格布局每个item的间距
         */
        mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                //item1距离底部的距离
                outRect.bottom = 30;
                //item距离左侧的距离
                outRect.left = 30;
            }
        });
        LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(getActivity());
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.intent.action.CART_BROADCAST");
        BroadcastReceiver mItemViewListClickReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent){
                Boolean msg = intent.getBooleanExtra("data",false);
                    mAdapter.refreshLight(msg);
                    /*被这里坑了*/
//                 Boolean   msg_m=intent.getBooleanExtra("musicdata",false);
//                    mAdapter.refreshMusic(msg_m);
//                Boolean   msg_a=intent.getBooleanExtra("acdndata",false);
//                mAdapter.refreshAcdn(msg_a);
            }
        };
        IntentFilter intentFilter_a = new IntentFilter();
        intentFilter_a.addAction("android.intent.action.CART_BROADCAST_a");
        BroadcastReceiver mItemViewListClickReceiver_a = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent){
                int room=intent.getIntExtra("roomid",0);
                boolean isall=intent.getBooleanExtra("isall",false);

                if(room==roomid||isall){
                Boolean   msg_a=intent.getBooleanExtra("acdndata",false);
                mAdapter.refreshAcdn(msg_a);}
            }
        };
        IntentFilter intentFilter_c = new IntentFilter();
        intentFilter_c.addAction("android.intent.action.CART_BROADCAST_c");
        BroadcastReceiver mItemViewListClickReceiver_c = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent){
                Boolean   msg_a=intent.getBooleanExtra("cdata",false);
                mAdapter.refreshc(msg_a);
            }
        };
        IntentFilter intentFilter_m = new IntentFilter();
        intentFilter_m.addAction("android.intent.action.CART_BROADCAST_m");
        BroadcastReceiver mItemViewListClickReceiver_m = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent){
                Boolean   msg_m=intent.getBooleanExtra("musicdata",false);
                mAdapter.refreshMusic(msg_m);
            }
        };
        IntentFilter intentFilter_door = new IntentFilter();
        intentFilter_door.addAction("android.intent.action.CART_BROADCAST_door");
        BroadcastReceiver mItemViewListClickReceiver_door = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent){
                Boolean   msg_door=intent.getBooleanExtra("mdata",false);
                mAdapter.refreshdoor(msg_door);
            }
        };
        IntentFilter intentFilter_add = new IntentFilter();
        intentFilter_add.addAction("android.intent.action.CART_BROADCAST_add");
        BroadcastReceiver mItemViewListClickReceiver_add = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent){
                int   roomi=intent.getIntExtra("roomid",0);
                System.out.println(roomid);
if(roomid==roomi){
                String   msg_add=intent.getStringExtra("mdata");
                String   devtag=intent.getStringExtra("devtag");
                int   devtype=intent.getIntExtra("devtype",-1);
                mAdapter.adddev(msg_add,devtag,devtype);
//    Toast to = Toast.makeText(context, devtag+"777", Toast.LENGTH_SHORT);
//    to.setGravity(Gravity.CENTER, 50, 0);
//    to.show();
}
            }
        };
        IntentFilter intentFilter_fenwei = new IntentFilter();
        intentFilter_fenwei.addAction("android.intent.action.CART_BROADCAST_fenwei");
        BroadcastReceiver mItemViewListClickReceiver_fenwei = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent){
                int room=intent.getIntExtra("roomid",0);
                boolean isall=intent.getBooleanExtra("isall",false);
                if(room==roomid||isall){
                    Boolean   msg_a=intent.getBooleanExtra("acdndata",false);
                    mAdapter.refreshefenwei(msg_a);}
            }
        };
        broadcastManager.registerReceiver(mItemViewListClickReceiver_add, intentFilter_add);
        broadcastManager.registerReceiver(mItemViewListClickReceiver_fenwei, intentFilter_fenwei);
        broadcastManager.registerReceiver(mItemViewListClickReceiver_door, intentFilter_door);

        broadcastManager.registerReceiver(mItemViewListClickReceiver, intentFilter);
        broadcastManager.registerReceiver(mItemViewListClickReceiver_a, intentFilter_a);
        broadcastManager.registerReceiver(mItemViewListClickReceiver_m, intentFilter_m);
        broadcastManager.registerReceiver(mItemViewListClickReceiver_c, intentFilter_c);
//        ImageView iv = view.findViewById(R.id.iv);
//        iv.setImageResource(argument.getInt(ARG_PARAM2,R.drawable.ji1));
        return view;
    }
}